package cn.stylefeng.guns.modular.system.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.system.entity.BasicDeliver;
import cn.stylefeng.guns.modular.system.model.params.BasicDeliverParam;
import cn.stylefeng.guns.modular.system.model.result.BasicDeliverResult;
import cn.stylefeng.guns.modular.system.service.BasicDeliverService;
import cn.stylefeng.guns.sys.core.constant.state.ManagerStatus;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.guns.sys.modular.system.entity.Dept;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;


/**
 * 交付管理表控制器
 *
 * @author zhucx
 * @Date 2019-06-25 11:00:01
 */
@Controller
@RequestMapping("/basicDeliver")
public class BasicDeliverController extends BaseController {

    private String PREFIX = "/system/basicDeliver";

    @Autowired
    private BasicDeliverService basicDeliverService;

    @Autowired
    private UserService userService;

    @Autowired
    private DeptService deptService;

    /**
     * 跳转到主页面
     *
     * @author zhucx
     * @Date 2019-06-25
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/basicDeliver.html";
    }

    /**
     * 新增页面
     *
     * @author zhucx
     * @Date 2019-06-25
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/basicDeliver_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zhucx
     * @Date 2019-06-25
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/basicDeliver_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zhucx
     * @Date 2019-06-25
     */
    @RequestMapping("/addItem")
    @ResponseBody
    @Transactional
    public ResponseData addItem(BasicDeliverParam basicDeliverParam,BindingResult result) {
    	//判断请求 400
        if (result.hasErrors()) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }

        // 判断账号是否重复
        User theUser = userService.getByAccount(basicDeliverParam.getSysuser());
        if (theUser != null) {
            throw new ServiceException(BizExceptionEnum.USER_ALREADY_REG);
        }
        
        //判断交付客户名称是否重复
        BasicDeliverParam Deliver = this.basicDeliverService.getBycustomername(basicDeliverParam.getCustomername());
        if (Deliver != null) {
            throw new ServiceException(BizExceptionEnum.Deliver_ALREADY_REG);
        }
        
        this.basicDeliverService.add(basicDeliverParam);
        BasicDeliverResult r = basicDeliverService.findBySpec(basicDeliverParam);
        
        //添加默认部门
        Dept dept = new Dept();
    	dept.setPid(0L);
    	dept.setPids("[0],");
    	dept.setSimpleName(r.getCustomername());
    	dept.setFullName(r.getCustomername());
    	dept.setSort(1);
    	dept.setDeliverId(Long.parseLong(r.getId().toString()));
    	deptService.addDept(dept);
    	Dept d = deptService.findBySpec(dept);

        // 完善账号信息
        String salt = ShiroKit.getRandomSalt(5);
        String password = ShiroKit.md5(basicDeliverParam.getSyspassword(), salt);
    	
        //创建用户实体类
        User user = new User();
        user.setRoleId("1143433644457250817");//系统管理员
        user.setAccount(basicDeliverParam.getSysuser());
        user.setEmail(basicDeliverParam.getMailbox());
        user.setPhone(basicDeliverParam.getTel());
        user.setCreateTime(new Date());
        user.setStatus(ManagerStatus.OK.getCode());
        user.setPassword(password);
        user.setSalt(salt);
        user.setDeliverId(Long.parseLong(r.getId().toString()));
        user.setDeliverName(r.getCustomername());
        user.setDeptId(d.getDeptId());
        userService.save(user);
        
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zhucx
     * @Date 2019-06-25
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BasicDeliverParam basicDeliverParam) {
        this.basicDeliverService.update(basicDeliverParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zhucx
     * @Date 2019-06-25
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BasicDeliverParam basicDeliverParam) {
        this.basicDeliverService.delete(basicDeliverParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhucx
     * @Date 2019-06-25
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BasicDeliverParam basicDeliverParam) {
        BasicDeliver detail = this.basicDeliverService.getById(basicDeliverParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author zhucx
     * @Date 2019-06-25
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BasicDeliverParam basicDeliverParam) {
        return this.basicDeliverService.findPageBySpec(basicDeliverParam);
    }

}


